<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>推文管理后台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        
        .header p {
            opacity: 0.9;
            font-size: 1.1em;
        }
        
        .form-container {
            padding: 40px;
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
            font-size: 1.1em;
        }
        
        .form-group input,
        .form-group textarea,
        .form-group select {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .form-group input:focus,
        .form-group textarea:focus,
        .form-group select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .form-group textarea {
            resize: vertical;
            min-height: 120px;
        }
        
        .btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 15px 30px;
            border: none;
            border-radius: 10px;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            width: 100%;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        .alert {
            padding: 15px;
            border-radius: 10px;
            margin-bottom: 20px;
            font-weight: 500;
        }
        
        .alert-success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        
        .alert-error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        
        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
        
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .form-row {
            display: flex;
            gap: 20px;
        }
        
        .form-row .form-group {
            flex: 1;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📝 推文管理后台</h1>
            <p>发布推文并实时推送给所有用户</p>
        </div>
        
        <div class="form-container">
            <div id="alertContainer"></div>
            
            <form id="postForm">
                <div class="form-group">
                    <label for="title">推文标题 *</label>
                    <input type="text" id="title" name="title" required placeholder="请输入推文标题">
                </div>
                
                <div class="form-group">
                    <label for="summary">推文摘要</label>
                    <textarea id="summary" name="summary" placeholder="请输入推文摘要（可选）"></textarea>
                </div>
                
                <div class="form-group">
                    <label for="content">推文内容 *</label>
                    <textarea id="content" name="content" required placeholder="请输入推文内容"></textarea>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label for="category">分类</label>
                        <select id="category" name="category">
                            <option value="">请选择分类</option>
                            <option value="技术">技术</option>
                            <option value="产品">产品</option>
                            <option value="公告">公告</option>
                            <option value="活动">活动</option>
                            <option value="其他">其他</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="authorName">作者名称 *</label>
                        <input type="text" id="authorName" name="authorName" required placeholder="请输入作者名称">
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label for="isFeatured">是否推荐</label>
                        <select id="isFeatured" name="isFeatured">
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="isTop">是否置顶</label>
                        <select id="isTop" name="isTop">
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                    </div>
                </div>
                
                <div class="loading" id="loading">
                    <div class="spinner"></div>
                    <p>正在发布推文...</p>
                </div>
                
                <button type="submit" class="btn" id="submitBtn">
                    🚀 发布推文
                </button>
            </form>
        </div>
    </div>

    <script>
        document.getElementById('postForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const submitBtn = document.getElementById('submitBtn');
            const loading = document.getElementById('loading');
            const alertContainer = document.getElementById('alertContainer');
            
            // 显示加载状态
            submitBtn.disabled = true;
            loading.style.display = 'block';
            alertContainer.innerHTML = '';
            
            // 收集表单数据
            const formData = new FormData(this);
            const postData = {
                title: formData.get('title'),
                summary: formData.get('summary'),
                content: formData.get('content'),
                category: formData.get('category'),
                authorName: formData.get('authorName'),
                authorId: 1, // 默认管理员ID
                isFeatured: parseInt(formData.get('isFeatured')),
                isTop: parseInt(formData.get('isTop'))
            };
            
            try {
                const response = await fetch('/api/posts/publish', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(postData)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showAlert('推文发布成功！已推送给所有用户。', 'success');
                    this.reset(); // 重置表单
                } else {
                    showAlert('发布失败：' + result.message, 'error');
                }
            } catch (error) {
                console.error('发布推文失败:', error);
                showAlert('发布失败：网络错误', 'error');
            } finally {
                // 隐藏加载状态
                submitBtn.disabled = false;
                loading.style.display = 'none';
            }
        });
        
        function showAlert(message, type) {
            const alertContainer = document.getElementById('alertContainer');
            const alertClass = type === 'success' ? 'alert-success' : 'alert-error';
            
            alertContainer.innerHTML = `
                <div class="alert ${alertClass}">
                    ${message}
                </div>
            `;
            
            // 3秒后自动隐藏
            setTimeout(() => {
                alertContainer.innerHTML = '';
            }, 3000);
        }
    </script>
</body>
</html>